﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    public static class DBBuddyRoster
    {
        //add buddy relationship between two accounts
        public static bool addBuddy(BuddyRoster br)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    // two-way relationship
                    BuddyRoster brInvert = new BuddyRoster();
                    brInvert.BuddyID1 = br.BuddyID2;
                    brInvert.BuddyID2 = br.BuddyID1;

                    db.AddToBuddyRosters(br);
                    db.AddToBuddyRosters(brInvert);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve buddy relationship by both buddy IDs
        public static BuddyRoster retrieveBuddy(string ID1, string ID2)
        {
            BuddyRoster buddy = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                buddy =
                (from a in db.BuddyRosters
                 where a.BuddyID1 == ID1 && a.BuddyID2 == ID2
                 select a).SingleOrDefault();
            }

            return buddy;
        }
        
        //retrieve all buddy relationships in a list for an account
        public static List<BuddyRoster> retrieveBuddyList(string accountID)
        {
            List<BuddyRoster> buddyList = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                buddyList =
                (from a in db.BuddyRosters
                 where a.BuddyID1 == accountID 
                 select a).ToList();
            }
            return buddyList;
        }

        //delete buddy relationship by both buddy IDs
        public static bool deleteBuddy(string ID1, string ID2)
        {
            try
            {
                BuddyRoster buddyToDelete = retrieveBuddy(ID1, ID2);
                BuddyRoster buddyToDeleteInvert = retrieveBuddy(ID2, ID1);

                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(buddyToDelete);
                    db.Attach(buddyToDeleteInvert);
                    db.DeleteObject(buddyToDelete);
                    db.DeleteObject(buddyToDeleteInvert);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //extract all IDs which are not the current account ID for the buddy name list
        public static IOrderedEnumerable<String> viewBuddyNameList(string ID)
        {
            List<BuddyRoster> buddyList = retrieveBuddyList(ID);
            List<String> nameList = null;
            Jobseeker jseeker = null;
            string name = "";

            foreach (BuddyRoster br in buddyList)
            {
                if (br.BuddyID1 != ID)
                {

                    using (hireddbEntities db = new hireddbEntities())
                    {
                        jseeker =
                        (from a in db.Jobseekers
                        where a.JAccountID == br.BuddyID1
                        select a).SingleOrDefault();
                    }
                }
                else if (br.BuddyID2 != ID)
                {
                    using (hireddbEntities db = new hireddbEntities())
                    {
                        jseeker =
                        (from a in db.Jobseekers
                        where a.JAccountID == br.BuddyID2
                        select a).SingleOrDefault();
                    }
                }

                name = jseeker.FirstName + " " + jseeker.LastName;
                nameList.Add(name);
            }

            var sorted = from n in nameList
                         orderby n ascending
                         select n;
            sorted.ToList<String>();
            return sorted;
        }

        //extract all IDs which are not the current account ID for the list with buddy name and image address
        public static List<Buddy> viewBuddyImageNameList(string ID)
        {
            List<BuddyRoster> buddyList = retrieveBuddyList(ID);
            List<string> idList = new List<string>();
            List<string> nameList = new List<string>();
            List<string> imageUrlList = new List<string>();
            List<string> usernameList = new List<string>();

            var query =
                from br in buddyList
                where br.BuddyID1 == ID
                select br;

            #region Retrieving values for each buddy
            foreach (BuddyRoster br in buddyList)
            {
                string buddyID = br.BuddyID2;

                using (hireddbEntities db = new hireddbEntities())
                {
                    switch (DBAccount.RetrieveAccountTypeByID(buddyID))
                    {
                        case "CompanyAccount":
                            var ca =
                                (from c in db.CompanyAccounts.Include("Company")
                                 where c.CAccountID == buddyID
                                 select c).SingleOrDefault();
                            nameList.Add(ca.Company.CompanyName);
                            imageUrlList.Add(ca.Company.ImageAddress);
                            usernameList.Add(ca.Username);
                            idList.Add(ca.CAccountID);
                            break;
                        case "JobseekerAccount":
                            var jsa =
                                (from j in db.JobseekerAccounts.Include("Jobseeker")
                                 where j.JAccountID == buddyID
                                 select j).SingleOrDefault();
                            nameList.Add(jsa.Jobseeker.FirstName + " " + jsa.Jobseeker.LastName);
                            imageUrlList.Add(jsa.Jobseeker.ImageAddress);
                            usernameList.Add(jsa.Username);
                            idList.Add(jsa.JAccountID);
                            break;
                    }
                }
            }
            #endregion

            List<Buddy> listToReturn = new List<Buddy>();
            for (int i = 0; i < buddyList.Count; i++)
            {
                Buddy b = new Buddy();
                b.UserID = idList[i];
                b.ImageAddress = imageUrlList[i];
                b.Name = nameList[i];
                b.Username = usernameList[i];
                listToReturn.Add(b);
            }

            return listToReturn;
        }

        //check if two accounts are already buddies
        public static bool checkBuddy(string ID1, string ID2)
        {
            try
            {
                List<BuddyRoster> buddyList = retrieveBuddyList(ID1);

                foreach (BuddyRoster br in buddyList)
                {
                    if (br.BuddyID1 == ID1 && br.BuddyID2 == ID2)
                    {
                        return false;
                    }
                    else if (br.BuddyID1 == ID2 && br.BuddyID2 == ID1)
                    {
                        return false;
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}